/*****************************************************************************
 *  CommLayerLib for Windowed Systems - LoggerFactory.java
 *  Copyright (C) 2012  Matteo Piscitelli
 *  E-mail: matteo@picciux.it
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *  For further info, check http://code.google.com/p/java-comm-layer/
 *
 *  SVN: $Id: LoggerFactory.java 43 2012-10-11 16:47:55Z picciux@gmail.com $
 *  
 *****************************************************************************/

package it.picciux.commlayer.win.log;

import java.io.IOException;
import java.io.OutputStream;

import it.picciux.commlayer.log.Logger;
import it.picciux.commlayer.log.LoggerException;
import it.picciux.commlayer.log.Logger.ILoggerFactory;

public class LoggerFactory implements ILoggerFactory {

	@Override
	public Logger getLoggerImpl(String moduleName, int type, Object[] params)
			throws LoggerException {
			
			Logger l = null;
			
			switch(type) {
					
				case Logger.CONSOLE:
					l = new ConsoleLogger(moduleName);
					break;
					
				case Logger.FILE:
					if (params == null || params.length == 0 || (!(params[0] instanceof String)))
						throw new LoggerException("Invalid Parameters");
					
					try {
						l = new FileLogger(moduleName, ((String) params[0]));
					} catch (SecurityException e) {
						throw new LoggerException("SecurityException", e);
					} catch (IOException e) {
						throw new LoggerException("IOException", e);
					}			
					break;
					
				case Logger.SOCKET:
					if (params == null || params.length < 2 || (!(params[0] instanceof String)) || (!(params[1] instanceof Integer)))
						throw new LoggerException("Invalid parameters");
					try {
						l = new SocketLogger(moduleName, ((String) params[0]), ((Integer) params[1]).intValue());
					} catch (IOException e) {
						throw new LoggerException("IOException", e);
					}
					break;
					
				case Logger.STREAM:
					if (params == null || params.length == 0 || (!(params[0] instanceof OutputStream)))
						throw new LoggerException("Invalid Parameters");
					l = new StreamLogger(moduleName, ((OutputStream) params[0]));
					break;
					
				case Logger.SERVER:
					if (params == null || params.length == 0 || (!(params[0] instanceof Integer)))
						throw new LoggerException("Invalid Parameters");
					try {
						l = new TCPServerLogger(moduleName, ((Integer) params[0]).intValue());
					} catch (IOException e) {
						throw new LoggerException("IOException", e);
					}
					break;
					
				default:
					throw new LoggerException("Logger type not implemented");
			}
			
			return l;
	}

}
